//
//  BDPostCommentCell.m
//  BadBoy
//
//  Created by wen on 2017/9/10.
//  Copyright © 2017年 com.8xiong. All rights reserved.
//

#import "BDPostCommentCell.h"
#import "BDPostHelper.h"

@implementation BDPostCommentProfileView{
    BOOL _trackingTouch;
}
- (instancetype)initWithFrame:(CGRect)frame {
    if (frame.size.width == 0 && frame.size.height == 0) {
        frame.size.width = kScreenWidth;
        frame.size.height = kBDCommentProfileHeight;
    }
    self = [super initWithFrame:frame];
    self.exclusiveTouch = YES;
    
    _avatarView = [UIImageView new];
    _avatarView.size = CGSizeMake(40, 40);
    _avatarView.origin = CGPointMake(kBDCommentCellPadding, kBDCommentCellPadding + 3);
    _avatarView.layer.masksToBounds = YES;
    _avatarView.layer.cornerRadius = _avatarView.width / 2.0;
    _avatarView.contentMode = UIViewContentModeScaleAspectFill;
    [self addSubview:_avatarView];
    
    CALayer *avatarBorder = [CALayer layer];
    avatarBorder.frame = _avatarView.bounds;
    avatarBorder.borderWidth = CGFloatFromPixel(1);
    avatarBorder.borderColor = [UIColor colorWithWhite:0.000 alpha:0.090].CGColor;
    avatarBorder.cornerRadius = _avatarView.height / 2;
    avatarBorder.shouldRasterize = YES;
    avatarBorder.rasterizationScale = kScreenScale;
    [_avatarView.layer addSublayer:avatarBorder];
    
    _avatarBadgeView = [UIImageView new];
    _avatarBadgeView.size = CGSizeMake(14, 14);
    _avatarBadgeView.center = CGPointMake(_avatarView.right - 6, _avatarView.bottom - 6);
    _avatarBadgeView.contentMode = UIViewContentModeScaleAspectFit;
    [self addSubview:_avatarBadgeView];
    
    _nameLabel = [YYLabel new];
    _nameLabel.size = CGSizeMake(kBDCommentNameWidth, 24);
    _nameLabel.left = _avatarView.right + kBDCommentNamePaddingLeft;
    _nameLabel.centerY = 27;
    _nameLabel.displaysAsynchronously = YES;
    _nameLabel.ignoreCommonProperties = YES;
    _nameLabel.fadeOnAsynchronouslyDisplay = NO;
    _nameLabel.fadeOnHighlight = NO;
    _nameLabel.lineBreakMode = NSLineBreakByClipping;
    _nameLabel.textVerticalAlignment = YYTextVerticalAlignmentCenter;
    [self addSubview:_nameLabel];
    
    _sourceLabel = [YYLabel new];
    _sourceLabel.frame = _nameLabel.frame;
    _sourceLabel.centerY = 47;
    _sourceLabel.displaysAsynchronously = YES;
    _sourceLabel.ignoreCommonProperties = YES;
    _sourceLabel.fadeOnAsynchronouslyDisplay = NO;
    _sourceLabel.fadeOnHighlight = NO;
    __weak BDPostCommentProfileView *weak_self = self;
    _sourceLabel.highlightTapAction = ^(UIView *containerView, NSAttributedString *text, NSRange range, CGRect rect) {
        if ([weak_self.cell.delegate respondsToSelector:@selector(cell:didClickInLabel:textRange:)]) {
            [weak_self.cell.delegate cell:weak_self.cell didClickInLabel:(YYLabel *)containerView textRange:range];
        }
    };
    [self addSubview:_sourceLabel];
    
    return self;
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    _trackingTouch = NO;
    UITouch *t = touches.anyObject;
    CGPoint p = [t locationInView:_avatarView];
    if (CGRectContainsPoint(_avatarView.bounds, p)) {
        _trackingTouch = YES;
    }
    p = [t locationInView:_nameLabel];
    if (CGRectContainsPoint(_nameLabel.bounds, p) && _nameLabel.textLayout.textBoundingRect.size.width > p.x) {
        _trackingTouch = YES;
    }
    if (!_trackingTouch) {
        [super touchesBegan:touches withEvent:event];
    }
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    if (!_trackingTouch) {
        [super touchesEnded:touches withEvent:event];
    } else {
        if ([_cell.delegate respondsToSelector:@selector(cell:didClickUser:)]) {
            [_cell.delegate cell:_cell didClickUser:_cell.layout.comment.user];
        }
    }
}

- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event {
    if (!_trackingTouch) {
        [super touchesCancelled:touches withEvent:event];
    }
}

@end

@interface BDPostCommentCell ()

@property (nonatomic, strong) UIView *topLine;
@property (nonatomic, strong) UIView *bottomLine;

@end

@implementation BDPostCommentCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    _profileView = [BDPostCommentProfileView new];
    [self.contentView addSubview:_profileView];
    
    _commentLabel = [YYLabel new];
    _commentLabel.clipsToBounds = YES;
    _commentLabel.left = kBDCommentCellPadding;
    _commentLabel.width = kBDCommentContentWidth;
    _commentLabel.textVerticalAlignment = YYTextVerticalAlignmentTop;
    _commentLabel.displaysAsynchronously = YES;
    _commentLabel.ignoreCommonProperties = YES;
    _commentLabel.fadeOnAsynchronouslyDisplay = NO;
    _commentLabel.fadeOnHighlight = NO;
    __weak BDPostCommentCell *weak_self = self;
    _commentLabel.highlightTapAction = ^(UIView *containerView, NSAttributedString *text, NSRange range, CGRect rect) {
//        if ([weak_self.cell.delegate respondsToSelector:@selector(cell:didClickInLabel:textRange:)]) {
//            [weak_self.cell.delegate cell:weak_self.cell didClickInLabel:(YYLabel *)containerView textRange:range];
//        }
    };
    [self.contentView addSubview:_commentLabel];
    
    _replyTableView = [[UITableView alloc] init];
    _replyTableView.delegate = self;
    _replyTableView.dataSource = self;
    _replyTableView.backgroundColor = kBDCommentBackgroundColor;
    _replyTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.contentView addSubview:_replyTableView];
    
    _topLine = [[UIView alloc] init];
    _topLine.backgroundColor = [UIColor colorWithHexString:@"#f2f2f2"];
    [self.contentView addSubview:_topLine];
    
    _bottomLine = [[UIView alloc] init];
    _bottomLine.backgroundColor = [UIColor colorWithHexString:@"#f2f2f2"];
    [self.contentView addSubview:_bottomLine];
    
    self.backgroundColor = [UIColor whiteColor];
    return self;
}

- (void)setLayout:(BDPostCommentLayout *)layout{
    _layout = layout;
    self.height = layout.height;
    self.contentView.height = layout.height;
    
    CGFloat top = 0;
    
    /// 圆角头像
    _topLine.size = CGSizeMake(self.width, 0.75);
    _topLine.top = 0;
    
    [_profileView.avatarView setImageWithURL:[NSURL URLWithString:layout.comment.user.cover] //profileImageURL
                                 placeholder:[UIImage imageNamed:@"avatar"]
                                     options:YYWebImageOptionIgnorePlaceHolder
                                     manager:[BDPostHelper avatarImageManager] // < 圆角头像manager，内置圆角处理
                                    progress:nil
                                   transform:nil
                                  completion:nil];
    
    
    _profileView.nameLabel.textLayout = layout.nameTextLayout;
    _profileView.sourceLabel.textLayout = layout.sourceTextLayout;
    
    _profileView.height = layout.profileHeight;
    _profileView.top = top;
    top += layout.profileHeight;
    
    _commentLabel.height = layout.commentHeight;
    _commentLabel.textLayout = layout.commentLayout;
    _commentLabel.top = top;
    _commentLabel.left = _profileView.nameLabel.left;
    top += layout.commentHeight;
    
    _replyTableView.size = CGSizeMake(kBDCommentContentWidth, layout.replyTableViewHeight);
    _replyTableView.top = top;
    _replyTableView.left = _profileView.nameLabel.left;
    _replyTableView.scrollEnabled = NO;
    [_replyTableView reloadData];
    
    _bottomLine.size = CGSizeMake(self.width, 0.75);
    _bottomLine.bottom = self.height;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _layout.replyHeight.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return _layout.replyHeight[indexPath.row].floatValue;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"postCommentCell"];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"postCommentCell"];
        YYLabel *replayLabel = [YYLabel new];
        replayLabel.size = CGSizeMake(cell.width - 10, cell.height);
        replayLabel.left = 5;
        replayLabel.clipsToBounds = YES;
        replayLabel.left = kBDCommentCellPadding;
        replayLabel.width = kBDCommentContentWidth;
        replayLabel.textVerticalAlignment = YYTextVerticalAlignmentTop;
        replayLabel.displaysAsynchronously = YES;
        replayLabel.ignoreCommonProperties = YES;
        replayLabel.fadeOnAsynchronouslyDisplay = NO;
        replayLabel.fadeOnHighlight = NO;
        replayLabel.tag = 3456;
        [cell.contentView addSubview:replayLabel];
        cell.backgroundColor = tableView.backgroundColor;
    }
    YYLabel *replayLabel = [cell viewWithTag:3456];
    replayLabel.textLayout = _layout.replyLayout[indexPath.row];
    return cell;
}
@end
